<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/8
 * Time: 17:43
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class My extends Base {
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['order_userid'] = $user_id;
        $where['order_status'] = 2;
        $res = Db::table('up_order')->where($where)->field('order_user_pay,order_number,order_type,order_paytime')->limit($limit*$num,$num)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //收藏记录
    public function userLike(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type')?input('param.type'):1;//1视频 2音频
        $num = 10;
        $limit = $page-1;
        if($type==1){//视频
            $where['a.is_type'] =1;
            $where['a.user_id'] = $user_id;
            $res = Db::table('up_like')->alias('a')
                ->join('up_video uv','uv.video_id = a.f_id','left')
                ->field('uv.is_vip,uv.video_id,uv.video_name,uv.video_img,uv.person_id')
                ->where($where)
                ->limit($limit*$num,$num)
                ->select();
            foreach ($res as $key=>&$vo){
                $personList = explode(',',trim($vo['person_id'],','));
                $person_name = Db::table('up_person')->where('person_id','in',$personList)->column('person_name');
                $vo['person_name'] = implode(',',$person_name);
            }
        }elseif ($type==2){//音频
            $where['a.is_type'] =2;
            $where['a.user_id'] = $user_id;
            $res = Db::table('up_like')->alias('a')
                ->join('up_music um','um.music_id = a.f_id','left')
                ->field('um.person_id,um.music_id,um.music_name,um.music_img,um.is_vip')
                ->where($where)
                ->limit($limit*$num,$num)
                ->select();
            foreach ($res as $key=>&$vo){
                $personList = explode(',',trim($vo['person_id'],','));
                $person_name = Db::table('up_person')->where('person_id','in',$personList)->column('person_name');
                $vo['person_name'] = implode(',',$person_name);
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    /**
     * 写入历史记录
     * @param number user_id
     * @param int media_type 1 video 2 music
     * @param int media_id 资源id，不分video或music
     * @param string create_time 浏览时间
     * return bool
     */
    public function historyWrite(){
        extract(input());
        $media_type = input('param.media_type');//1视频 2音频
        $children_id = input('param.children_id');//视频集的ID
        $media_id = input('param.media_id');//视频或者音频ID
        $histroy_time = input('param.histroy_time');//视频观看的时间
        if($media_type == 1){//视频
            $where['user_id'] = $user_id;
            $where['media_id'] = $media_id;
            $where['media_type'] = 1;
            $res =  Db::table('up_history')->where($where)->find();
            if($res['histroy_id']==''){//不存在该视频
                $data['user_id'] = $user_id;
                $data['media_id'] = $media_id;
                $data['media_type'] = 1;
                $data['children_id'] = $children_id;
                $data['create_time'] = date('Y-m-d H:i:s');
                $data['histroy_time'] = $histroy_time;
                $info = Db::table('up_history')->insertGetId($data);
            }else{//存在的话更新浏览时间
                $data['create_time'] = date('Y-m-d H:i:s');
                $data['histroy_time'] = $histroy_time;
                $data['children_id'] = $children_id;
                $info = Db::table('up_history')->where('histroy_id',$res['histroy_id'])->update($data);
            }
        }elseif ($media_type==2){//音频
            $where['user_id'] = $user_id;
            $where['media_id'] = $media_id;
            $where['media_type'] = 2;
           $res =  Db::table('up_history')->where($where)->find();
            if($res['histroy_id']==''){//不存在该音频
                $data['user_id'] = $user_id;
                $data['media_id'] = $media_id;
                $data['media_type'] = 2;
                $data['children_id'] = $children_id;
                $data['create_time'] = date('Y-m-d H:i:s');
                $data['histroy_time'] = $histroy_time;
                $info = Db::table('up_history')->insertGetId($data);
            }else{//存在的话更新浏览时间
                $data['create_time'] = date('Y-m-d H:i:s');
                $data['histroy_time'] = $histroy_time;
                $data['children_id'] = $children_id;
                $info = Db::table('up_history')->where('histroy_id',$res['histroy_id'])->update($data);
            }
        }
        if($info){
            return json(['status' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['status' => 200, 'msg' => "操作失败"]);
        }
    }

    /**
     * 访问历史记录 音频
     * @param number user_id
     * @param int media_type 1 video 2 music
     * @param number limits
     * @param number nowPage
     * return bool
     */
    public function history_music_list()
    {
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['a.media_type'] =2;
        $where['a.user_id'] = $user_id;
        $res = Db::table('up_history')->alias('a')
            ->join('up_music um','um.music_id = a.media_id','left')
            ->field('a.children_id,um.person_id,a.media_id,um.music_id,um.music_name,um.music_img,um.is_vip')
            ->where($where)
            ->limit($limit*$num,$num)
            ->select();
        foreach ($res as $key => &$value) {
            $whereMap['user_id'] = $user_id;
            $whereMap['is_type'] = 2;
            $whereMap['f_id'] = $value['media_id'];
            $is_like = Db::table('up_like')->where($whereMap)->find();
            if(!$is_like) {
                $value['is_like'] = 2;
            }else {
                $value['is_like'] = 1;
            }
            $chirden= Db::table('up_music_chirden')->where('chirden_id',$value['children_id'])->field('chirden_url,chirden_title')->find();
            $vo['hirden_title'] =$chirden['chirden_title'];
            $vo['chirden_url'] =$chirden['chirden_url'];
            $personList = explode(',',trim($value['person_id'],','));
            $person_name = Db::table('up_person')->where('person_id','in',$personList)->column('person_name');
            $vo['person_name'] = implode(',',$person_name);

        }
        return json(['status' => 200, 'msg' => $res]);
    }

    /*
    * 访问历史记录 视频
    * @param number user_id
    * @param int media_type 1 video 2 music
    * @param number limits
    * @param number nowPage
    * return bool
    */
    public function history_video_list()
    {
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
            $where['a.media_type'] =1;
            $where['a.user_id'] = $user_id;
            $res = Db::table('up_history')->alias('a')
                ->join('up_video uv','uv.video_id = a.media_id','left')
                ->field('uv.is_vip,uv.person_id,a.histroy_time,a.children_id,uv.video_id,uv.video_name,uv.video_img')
                ->where($where)
                ->limit($limit*$num,$num)
                ->select();
        foreach ($res as $key=>&$vo){
            $chirden= Db::table('up_video_chirden')->where('chirden_id',$vo['children_id'])->field('chirden_url,chirden_title')->find();
            $vo['hirden_title'] =$chirden['chirden_title'];
            $vo['chirden_url'] =$chirden['chirden_url'];
            $personList = explode(',',trim($vo['person_id'],','));
            $person_name = Db::table('up_person')->where('person_id','in',$personList)->column('person_name');
            $vo['person_name'] = implode(',',$person_name);
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    //背景图片
    public function banner1(){
        $res =  Db::table('up_backdrop')->where('backdrop_status',4)->find();
        return json(['status' => 200, 'msg' => $res]);
    }
}